package com.e1xp.deport_management_system.service.impl;

import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.exception.CustomExceptionType;
import com.e1xp.deport_management_system.mapper.AnalyseMapper;
import com.e1xp.deport_management_system.service.IAnalyseService;
import com.e1xp.deport_management_system.utils.AnalyseData;
import com.e1xp.deport_management_system.utils.ProductDataWrapper;
import com.e1xp.deport_management_system.utils.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author E1xP@foxmail.com
 * @version 1.0
 * @PACKAGE_NAME com.e1xp.deport_management_system.service.impl
 * @CLASS_NAME AnalyseServiceImpl
 * @Description TODO 分析数据业务类
 * @Date 2021/5/17 下午 1:26
 **/
@Service
public class AnalyseServiceImpl implements IAnalyseService {

    @Resource
    AnalyseMapper analyseMapper;

    /**
     * 用于获取统计数据
     *
     * @param instanceId 实例id
     * @param startTime  开始时间
     * @param endTime    结束事件
     * @return Map 返还信息
     * @throws CustomException 当结束时间早于开始时间时抛出该异常
     */
    @Override
    public Map<String, Object> getData(long instanceId, LocalDate startTime, LocalDate endTime) throws CustomException {
        Map<String, Object> returnMap = new HashMap<>();
        if (startTime.isAfter(endTime)) {
            //结束时间早于开始时间
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "结束时间在开始时间之前");
        }
        //获取时间列表
        List<String> dateList = Utils.getDateList(startTime, endTime);
        returnMap.put("dateList", dateList);
        //获取入库数据
        List<AnalyseData> inCountList = analyseMapper.getInCount(instanceId, startTime, endTime);

        List<Integer> inData = Utils.formatData(dateList, inCountList);
        returnMap.put("inData", inData);
        //获取出库数据
        List<AnalyseData> outCountList = analyseMapper.getOutCount(instanceId, startTime, endTime);
        List<Integer> outData = Utils.formatData(dateList, outCountList);
        returnMap.put("outData", outData);
        //获取订单统计
        List<AnalyseData> orderCountList = analyseMapper.getOrderCount(instanceId, startTime, endTime);
        List<Integer> orderIncreaseData = Utils.formatData(dateList, orderCountList);
        returnMap.put("orderIncreaseData", orderIncreaseData);
        //获取商品统计
        List<ProductDataWrapper> productCountList = analyseMapper.getProductSellCount(instanceId, startTime, endTime);
        List<Integer> pieValueList = productCountList.stream().map(ProductDataWrapper::getCount).collect(Collectors.toList());
        List<String> pieLabelList = productCountList.stream().map(ProductDataWrapper::getName).collect(Collectors.toList());
        returnMap.put("pieValueList", pieValueList);
        returnMap.put("pieLabelList", pieLabelList);
        return returnMap;
    }
}
